package com.hjj.controller;

import com.hjj.beans.ActivitiesRemark;
import com.hjj.beans.User;
import com.hjj.dto.ResultDTO;
import com.hjj.exception.CRUDException;
import com.hjj.exception.DataVerificationException;
import com.hjj.services.ActivitiesRemarkServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Locale;

@RestController
@RequestMapping("/ActivitiesRemark")
public class ActivitiesRemarkController {
    @Autowired
    ActivitiesRemarkServices activitiesRemarkServices;

    @Autowired
    MessageSource messageSource;


    @RequestMapping("add.action")
    public ResultDTO add(@Valid ActivitiesRemark activitiesRemark, BindingResult bindingResult, HttpSession httpSession) throws CRUDException, DataVerificationException {
        // 使用jsr303验证器验证前端传送的参数
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();

            // 获取错误消息资源中的消息信息
            throw new DataVerificationException(messageSource.getMessage(message, null, Locale.getDefault()));
        }


        //获取当前登录者信息
        User user = (User) httpSession.getAttribute("user");

        // 设置备注创建者
        activitiesRemark.setNotePerson(user.getName());

        boolean add = activitiesRemarkServices.add(activitiesRemark);

        if (!add) throw new CRUDException("备注添加失败");

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }


    @RequestMapping("edit.action")
    public ResultDTO edit(@Valid ActivitiesRemark activitiesRemark, BindingResult bindingResult, HttpSession httpSession) throws CRUDException, DataVerificationException {
        // 使用jsr303验证器验证前端传送的参数
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();

            // 获取错误消息资源中的消息信息
            throw new DataVerificationException(messageSource.getMessage(message, null, Locale.getDefault()));
        }


        //获取当前登录者信息
        User user = (User) httpSession.getAttribute("user");

        ActivitiesRemark temp = activitiesRemarkServices.get(activitiesRemark.getId());

        if (!user.getName().equals(temp.getNotePerson())) throw new CRUDException("该备注不是你创建的，你没有修改权限");

        // 设置备注创建者
        activitiesRemark.setEditPerson(user.getName());

        boolean edit = activitiesRemarkServices.edit(activitiesRemark);

        if (!edit) throw new CRUDException("备注修改失败");

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }

    @RequestMapping("del.action")
    public ResultDTO del(String id, HttpSession httpSession) throws CRUDException {
        // 获取要删除的评论信息
        ActivitiesRemark activitiesRemark = activitiesRemarkServices.get(id);

        // 获取当前登录者的用户信息
        User user = (User) httpSession.getAttribute("user");

        if (!user.getName().equals(activitiesRemark.getNotePerson())) throw new CRUDException("该备注不是你创建的，你没有删除权限");

        boolean del = activitiesRemarkServices.del(id);

        if (!del) throw new CRUDException("备注删除失败");

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }
}
